﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace textDemo
{
    public partial class Form1 : Form
    {

        private void DisplayRowColInfo()//显示光标所在的行和列的信息
        {
            int index = textBox1.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引
            int line = textBox1.GetLineFromCharIndex(index) + 1;//得到当前行的行号,从0开始，习惯是从1开始，所以+1.
            int col = textBox1.SelectionStart - index + 1; //.SelectionStart得到光标所在位置的索引 减去 当前行第一个字符的索引 = 光标所在的列数（从0开始)

            statusBar_Row.Text = line.ToString() + " 行";
            statusBar_Col.Text = col.ToString() + " 列";
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 格式ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            DisplayRowColInfo();
        }

        private void textBox1_MouseUp(object sender, MouseEventArgs e)
        {
            DisplayRowColInfo();
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr;
                sr = new System.IO.StreamReader(openFileDialog1.FileName, Encoding.UTF8);
                textBox1.Text = sr.ReadToEnd();
                sr.Close();
                _FileName = openFileDialog1.FileName;
                this.Text = _FileName;
                _IsSaved = true;
            }

        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
          
        }

         private void SaveTextFile(string fileName)
        {
            StreamWriter sw = new StreamWriter(fileName);
            sw.WriteLine(textBox1.Text);
            sw.Flush();//文件流
            sw.Close();//最后要关闭写入状态
            _IsSaved = true;
            this.Text = _FileName;
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (_FileName == "")
                _FileName = this.Text;
 
            saveFileDialog1.FileName = _FileName;
            if(saveFileDialog1.ShowDialog()==DialogResult.OK)
            {
                _FileName = saveFileDialog1.FileName;
                SaveTextFile(_FileName);
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }
    }
}
